package net.ranran.action;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.security.auth.message.callback.PrivateKeyCallback.Request;
import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.struts2.ServletActionContext;
import org.junit.jupiter.api.Test;

import com.opensymphony.xwork2.ActionSupport;

import net.ranran.pojo.sys_roominfo;
import net.ranran.pojo.sys_user;
import net.ranran.utils.SqlSessionFactoryUtils;
/**
 * 登录界面的action
 * @author Mustang
 *
 */
public class loginAction extends ActionSupport{
	private String loginName;
	private String loginPwd;
	
	
	public String getLoginName() {
		return loginName;
	}


	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}


	public String getLoginPwd() {
		return loginPwd;
	}


	public void setLoginPwd(String loginPwd) {
		this.loginPwd = loginPwd;
	}
	//继承ActionSupport后重写数据校验的方法
	@Override
		public void validate() {
		
		//判断账户不能为空
			if(loginName == null || loginName.trim().length() == 0) {
				//组织execute方法执行
				this.addFieldError("loginName", "用户名不能为空");
			}
		//判断密码不能为空
			if(loginPwd == null || loginPwd.trim().length() == 0) {
				//组织execute方法执行
				this.addFieldError("loginPwd", "密码不能为空");
			}
			
			
			
			
		}
	//针对方法校验数据
	public void validatelogin() {
		if(loginPwd.length() < 6 || loginPwd.length()>12) {
			this.addFieldError("loginPwd", "密码需要在6-12位之间");
		}
	}
	
	@Override
	public String execute() throws Exception {
		SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();

		SqlSession sqlSession = sqlSessionFactory.openSession();
	    sys_user user = sqlSession.selectOne("sys_user.getUserById",1);
	    System.out.println(user);
	    System.out.println(user.getSU_Password());
	    
		
		sqlSession.close();
		if(loginName == user.getSU_LoginName() || loginPwd == user.getSU_Password() ) {
			//组织execute方法执行
			System.out.println(loginName+"+++"+loginPwd);
			this.addFieldError("login", "用户名或者密码不匹配");
		}
		// TODO Auto-generated method stub
		System.out.println("loginName= >"+loginName+"---loginPwd= >"+loginPwd);
//		HttpServletRequest request = ServletActionContext.getRequest();
//		request.setAttribute("username", "sys_user");
		return "homePage";
	}
	
	@Test
	/**
	 * mybatis查询sys_user表
	 * @throws Exception
	 */
	public void getUserById() throws Exception {
//		SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
		 SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder(); InputStream
		 inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
		 SqlSessionFactory sqlSessionFactory = ssfb.build(inputStream);
		 
		
		SqlSession sqlSession = sqlSessionFactory.openSession();
	    sys_user user = sqlSession.selectOne("sys_user.getUserById",1);
	    System.out.println(user);
	    System.out.println(user.getSU_Password());
		
		sqlSession.close();
	}
	
	@Test
	/**
	 * mybatis查询sys_roominfo表
	 * @author Mustang
	 */
	public void getroominfoById() {
		SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
		
		SqlSession sqlSession = sqlSessionFactory.openSession();
		sys_roominfo roominifo  = sqlSession.selectOne("sys_roominfo.getroominfoById",1);
		System.out.println(roominifo);
		
		sqlSession.close();
	}
	
	
	
	
	
	
	
}
